<template>
  <div class="login-wrap"
       v-loading="isLogin">
    <div class="login-center">
      <!-- 请从wps登录系统-->
      <div class="fx_setbox ui-alignCenter mb20 mt20"
           v-show="!isLogin">
        <div class="fx_boxcnt">
          <div class="fx_boxcnt_bt clearfix">
            <div class="fx_additional-info">请从WPS打开或用参数jwt登录</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import './static/login.css'
import { mapGetters, mapMutations } from 'vuex'
import { GetUserInfoByjwt, getCityId, login } from './Resources/Api/index'
// 测试
// var clpurl = 'http://192.168.1.68:9101/api'
// 线上
// var clpurl = 'https://gapi.rxjy.com/api'
// var myInterval = null
function setCookie (name, value) {
    var exp = new Date()
    exp.setTime(exp.getTime() + 3 * 60 * 60 * 1000)
    document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
}
export default {
    metaInfo: {
        title: '施工店铺',
        titleTemplate: '%s'
    },
    data () {
        return {
            query: {},
            isLogin: true
            // areaID: null
        }
    },
    computed: {
        ...mapGetters(['userInfo', 'rx'])
    },
    created () {
        this.query = this.$route.query
        // 是否显示头部和侧栏
        if (this.query.cutting || this.query.role) {
            this.setRx({
                cutting: this.query.cutting,
                loginRole: this.query.role
            })
        }
        // 使用jwt登陆
        if (this.query.jwt || this.userInfo.as_jwt) {
            const jwt = this.query.jwt ? this.query.jwt : this.userInfo.as_jwt
            if (jwt) {
                this.login(jwt)
            } else {
                this.isLogin = false
            }
        } else if (this.query.uCard) {
            this.cardLogin()
        } else {
            this.isLogin = false
        }
        // 使用免登录地址
        // if (this.query.jwt) {
        //     const jwt = this.query.jwt
        //     if (this.query.areaID) {
        //         this.getAreaID(jwt)
        //     } else {
        //         this.login(jwt)
        //     }
        // } else if (this.query.uCard) {
        //     this.cardLogin()
        // } else {
        //     const jwt = this.userInfo ? this.userInfo.as_jwt : ''
        //     if (jwt) {
        //         if (this.query.areaID) {
        //             this.getAreaID(jwt)
        //         } else {
        //             this.login(jwt)
        //         }
        //     } else {
        //         this.isLogin = false
        //     }
        // }
    },
    methods: {
        ...mapMutations({
            setRx: 'SET_RX',
            setUserInfo: `SET_USER_INFO`
        }),
        // async getAreaID (jwt) {
        //     let params = {
        //         areaID: this.query.areaID
        //     }
        //     await getCityId(params).then((res) => {
        //         if (res.data.StatusCode === 1) {
        //             this.areaID = res.data.Body.cityID
        //             this.login(jwt)
        //         }
        //     }).catch((error) => {
        //         console.log(error)
        //     })
        // },
        // 选项卡切换
        // tabIdChange (index) {
        //     this.tabId = index
        // },
        async login (jwt) {
            this.isLogin = false
            let _this = this
            // let tempFlag = this.query.tempFlag
            let parm = {
                jwt: jwt
            }
            await GetUserInfoByjwt(parm).then((res) => {
                if (res.data.StatusCode === 0) {
                    setCookie('u_diqu', res.data.Body.u_diqu)
                    setCookie('diquName', res.data.Body.diquName)
                    setCookie('u_kahao', res.data.Body.u_kahao)
                    setCookie('xinming', res.data.Body.xinming)
                    setCookie('juesename', res.data.Body.juesename)
                    setCookie('juese', res.data.Body.juese)
                    setCookie('touxiang', res.data.Body.touxiang)
                    // 将数据存到UserInfo中
                    let results = {}
                    results.as_userImg = res.data.Body.touxiang// 头像
                    results.as_diquName = res.data.Body.diquName // 地区名称
                    results.as_diquId = res.data.Body.u_diqu // 地区id
                    results.as_jueseName = res.data.Body.juesename // 角色
                    results.as_fullName = res.data.Body.xinming // 姓名
                    results.as_cardNo = res.data.Body.u_kahao // 卡号
                    results.as_juese = res.data.Body.juese // 角色
                    results.as_timeStamp = '' // 登录时间
                    results.as_jwt = jwt // jwt
                    // results.as_areaId = _this.areaID
                    results.as_level = []
                    // results.as_role = _this.rx.loginRole === undefined ? 0 : Number(_this.rx.loginRole) // 登陆角色
                    this.setUserInfo({ ...results })
                    let params = {
                        areaID: res.data.Body.u_diqu,
                        userNo: res.data.Body.u_kahao
                    }
                    getCityId(params).then((res) => {
                        let role
                        if (res.data.StatusCode === 1) {
                            // 判断当前的角色 0 集团 1 是监理 2. 城登陆 3.工程经理 4.其他角色
                            if (Number(res.data.Body.u_diqu) !== 39) {
                                if (Number(this.rx.loginRole) === 2) {
                                    role = 2
                                } else {
                                    const level = res.data.Body.level
                                    if (level.length > 0) {
                                        if (level.includes('47')) {
                                            role = 3
                                        } else if (level.includes('82') || level.includes('83')) {
                                            role = 1
                                        } else {
                                            role = 4
                                        }
                                    } else {
                                        role = 4
                                    }
                                }
                            } else {
                                role = 0
                            }
                        } else {
                            role = 0
                        }
                        const userInfo = this.userInfo
                        this.setUserInfo({
                            ...userInfo,
                            'as_level': res.data.Body.level,
                            'as_cityId': res.data.Body.cityID,
                            'as_role': this.rx.loginRole !== undefined ? Number(this.rx.loginRole) : role
                        })
                        if (this.rx.currentRoute) {
                            _this.$router.push(this.rx.currentRoute)
                        } else {
                            _this.$router.push('/projectFiles/Project')
                        }
                    }).catch((error) => {
                        console.log(error)
                    })
                    // switch (tempFlag) {
                    // case '1':
                    //     _this.$router.push('/projectFiles/Department')
                    //     break
                    // case '2':
                    //     _this.$router.push('/projectFiles/Term')
                    //     break
                    // default:
                    //     _this.$router.push('/projectFiles/Project')
                    // }
                }
            }).catch((error) => {
                console.log(error)
            })
        },
        // 卡号登陆
        cardLogin () {
            let _this = this
            // let tempFlag = this.query.tempFlag
            login({userNo: this.query.uCard}).then((res) => {
                if (res.data.StatusCode === 1) {
                    setCookie('u_diqu', res.data.Body.userInfo.u_diqu)
                    setCookie('diquName', res.data.Body.userInfo.dq_name)
                    setCookie('u_kahao', res.data.Body.userInfo.u_kahao)
                    setCookie('xinming', res.data.Body.userInfo.u_xingming)
                    setCookie('juesename', res.data.Body.userInfo.js_name)
                    setCookie('touxiang', res.data.Body.userInfo.u_datouxiang)
                    // 将数据存到UserInfo中
                    let results = {}
                    results.as_userImg = res.data.Body.userInfo.u_datouxiang // 头像
                    results.as_diquName = res.data.Body.userInfo.dq_name // 地区名称
                    results.as_diquId = res.data.Body.userInfo.u_diqu // 地区id
                    results.as_jueseName = res.data.Body.userInfo.js_name// 角色
                    results.as_fullName = res.data.Body.userInfo.u_xingming // 姓名
                    results.as_cardNo = res.data.Body.userInfo.u_kahao // 卡号
                    results.as_areaId = _this.areaID
                    // results.as_level = []
                    // results.as_role = _this.rx.loginRole === undefined ? 0 : Number(_this.rx.loginRole) // 登陆角色
                    this.setUserInfo({ ...results })
                    let params = {
                        areaID: res.data.Body.userInfo.u_diqu,
                        userNo: res.data.Body.u_kahao
                    }
                    getCityId(params).then((res) => {
                        if (res.data.StatusCode === 1) {
                            const userInfo = this.userInfo
                            this.setUserInfo({
                                ...userInfo,
                                'as_level': res.data.Body.level,
                                'as_cityId': res.data.Body.cityID,
                                as_role: this.rx.loginRole !== undefined ? Number(this.rx.loginRole) : 1
                            })
                            if (this.rx.currentRoute) {
                                _this.$router.push(this.rx.currentRoute)
                            } else {
                                _this.$router.push('/projectFiles/Project')
                            }
                        }
                    }).catch((error) => {
                        console.log(error)
                    })

                    // switch (tempFlag) {
                    // case '1':
                    //     _this.$router.push('/projectFiles/Department')
                    //     break
                    // case '2':
                    //     _this.$router.push('/projectFiles/Term')
                    //     break
                    // default:
                    //     _this.$router.push('/projectFiles/Project')
                    // }
                }
            }).catch((error) => {
                console.log(error)
            })
        }
    }
}

</script>
<style scoped>
.login-wrap {
  position: fixed !important;
  width: 100%;
  height: 100%;
  top: 0px;
  left: 0px;
  background-color: #fff;
  z-index: 10000;
}
.login-center {
  position: absolute;
  width: 647px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -80%);
}
.login-text {
  text-align: center;
}
.fx_boxcnt_bt {
  background: #009688;
}
.fx_additional-info {
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
  line-height: 110px;
}
</style>
